import got from "got";
import { readFileByType } from "./util";
import qs from "qs";
import { delThoughts } from "./delThoughts";

const prefixUrl = process.env.ICAN_PREFIXURL;
const getThoughtsUrl = process.env.ICAN_GET_THOUGHTS;
const token = readFileByType('access_token')
const user_id = readFileByType('user_id')

class PageDTO {
  constructor(size: number, id: number) {
    this.size = size
    this.id = id
  }
  current = 1
  size: number
  id: number
}

const getThoughts = async () => {
  const headers = {
    authorization: `Bearer ${token}`
  }
  const pageDto = new PageDTO(1000, +user_id)
  const params = qs.stringify(pageDto);
  const postUrl = `${prefixUrl}${getThoughtsUrl}?${params}`;
  try {
    const res = await got.get(postUrl, {
      headers,
    });
    console.log(`获取成功`);
    const jsonData = res.body
    const data = JSON.parse(jsonData)
    const ids = data.data.records.map((m: any) => m.thoughtsId).filter((m: any) => m > 9000)
    console.log(ids);
    delThoughts(ids)
    return res;
  } catch (error) {
    console.error(error);
  }
}

export { getThoughts }